package com.zzyl.task;

import com.zzyl.mapper.CustomerReservationMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

/**
 * @author sjqn
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class MyTask {

    private final CustomerReservationMapper customerReservationMapper;
    /**
     * 定时任务 每小时的1分钟和31分钟自动检查是否有过时的预约，如果有，则改为已过期
     */
    @Scheduled(cron = "* 1/30 * * * ?")
    public void executeTask() throws InterruptedException {
        log.info("定时任务开始执行：{},将过期的预约状态改为3", LocalDateTime.now());
        LocalDateTime time = LocalDateTime.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedTime = time.format(formatter);

        customerReservationMapper.changeOverTime(formattedTime);
        Thread.sleep(60000);
    }
}